/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.BankAccount;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.repository.BankAccountRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;


/**
 *
 * @author ronildo
 */
public class BankAccountDAO extends GenericDAO<BankAccount, BankAccountRepository> implements BankAccountRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public BankAccount findById(Long id) {
        return (BankAccount)em.createNamedQuery("BankAccount.findByPkBankAccount").
                setParameter("pkBankAccount", id).getSingleResult();
    }

    public BankAccount findById(BankAccount bankAccount) {
        return (BankAccount)em.createNamedQuery("BankAccount.findByPkBankAccount").
                setParameter("pkBankAccount", bankAccount.getPkBankAccount()).getSingleResult();
    }

    public BankAccount findByName(String name) {
        try{
            return (BankAccount)em.createNamedQuery("BankAccount.findByName").
                setParameter("name", name).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find BankAccount for this name: " + name);
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<BankAccount> list(BankAccount bankAccount){
        try{
            Company company = bankAccount.getCompany();
            String hql = "select bankAccount from BankAccount as bankAccount ";
            hql+=" left outer join bankAccount.company as company";
            hql+=" where bankAccount.pkBankAccount = bankAccount.pkBankAccount";

            if(company != null && company.getPkCompany() != null)
                hql+=" and company.pkCompany = :pkCompany";
            if(company != null && company.getName() != null && !company.getName().equals(""))
                hql+=" and company.name = :company_name";
            if(bankAccount.getName() != null && !bankAccount.getName().equals(""))
                hql+=" and bankAccount.name = :bankAccount_name";
            hql+=" order by bankAccount.name";

            Query query = em.createQuery(hql);
            if(company != null && company.getPkCompany() != null)
                query.setParameter("pkCompany", company.getPkCompany());
            if(company != null && company.getName() != null && !company.getName().equals(""))
                query.setParameter("company_name", company.getName());
            if(bankAccount.getName() != null && !bankAccount.getName().equals(""))
                query.setParameter("bankAccount_name", bankAccount.getName());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list bank account",e);
            throw new DAOException(e);
        }
    }

    public List<BankAccount> listAll(){
        try{
            return em.createNamedQuery("BankAccount.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }
}
